package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import controller.CloseListener;
import controller.OpenListener;
import controller.ScrollListener;

public class AppFrame extends JFrame {
    private static final long serialVersionUID = -8886996135075758811L;

    private static AppFrame singletonInstance = new AppFrame();

    private StatusBar statusBar = new StatusBar(); // status bar
    private ToolBar toolBar = new ToolBar(); // status bar

    private SVGPanel SVGPanel = new SVGPanel(); // Make a panel

    public AppFrame() {

	addMenuBar(); // Insert menu bar items
	add(statusBar, BorderLayout.SOUTH); // Add status bar
	add(toolBar, BorderLayout.NORTH); // add tool bar

	// Scroll pane, with policies
	JScrollPane panelView = new JScrollPane(SVGPanel,
		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

	panelView.getHorizontalScrollBar().addAdjustmentListener(
		new ScrollListener());
	panelView.getVerticalScrollBar().addAdjustmentListener(
		new ScrollListener());

	panelView.setBackground(Color.WHITE); // set background color
	add(panelView, BorderLayout.CENTER); // add scroll pane to frame's
					     // centre

	// Frame
	setSize(840, 600); // size
	setMinimumSize(new Dimension(400, 300)); // minimum size
	setTitle("SVG Viewer"); // title
	setLocationRelativeTo(null); // relative to screen - centre
	setVisible(true);

    }

    // the menu bar
    private void addMenuBar() {

	JMenuBar jmbBar = new JMenuBar();

	// File menu, items and their listeners
	JMenu jmFile = new JMenu("File");

	JMenuItem jmiOpen = new JMenuItem("Open...");
	jmiOpen.addActionListener(new OpenListener());

	JMenuItem jmiExit = new JMenuItem("Exit");
	jmiExit.addActionListener(new CloseListener());

	// add items to their menus
	jmbBar.add(jmFile);
	jmFile.add(jmiOpen);
	jmFile.addSeparator();
	jmFile.add(jmiExit);

	setJMenuBar(jmbBar); // set frames menu bar

    }

    public StatusBar getStatusBar() {
	return statusBar;
    }

    public ToolBar getToolBar() {
	return toolBar;
    }

    public SVGPanel getSVGPanel() {
	return SVGPanel;
    }

    // singleton instance
    public static AppFrame getSingletonInstance() {
	return singletonInstance;
    }
}
